// SPDX-License-Identifier: Unlicense OR MIT

package main

// A simple Gio program. See https://gioui.org for more information.

import (
	"gioui.org/unit"
	"gioui.org/widget"
	"log"
	"os"
	"strconv"

	"gioui.org/app"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/widget/material"
)

func main() {
	go func() {
		w := new(app.Window)
		if err := loop(w); err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

type (
	C = layout.Context
	D = layout.Dimensions
)

func loop(w *app.Window) error {
	th := material.NewTheme()
	var (
		ops      op.Ops
		menuBtn  widget.Clickable
		btn      widget.Clickable
		checkbox widget.Bool
		editor   widget.Editor
		// 添加菜单状态变量
		menuOpen   bool
		menuItems  = []string{"选项1", "选项2", "选项3"}
		itemClicks []widget.Clickable

		termInput  widget.Editor
		termOutput widget.Editor
		termBtn    widget.Clickable
	)

	// 初始化菜单项点击处理器
	itemClicks = make([]widget.Clickable, len(menuItems))

	editor.SetText("在这里输入文本...")
	termWindow := new(app.Window)
	// 启动终端窗口
	editor.SetText("在这里输入文本...")
	termInput.SetText("> ")
	termOutput.SetText("终端输出:\n")
	termOutput.ReadOnly = true
	for {

		//switch e := w.Event().(type) {
		switch e := termWindow.Event().(type) {
		case app.DestroyEvent:
			return e.Err
		case app.FrameEvent:
			gtx := app.NewContext(&ops, e)
			if menuBtn.Clicked(gtx) {
				menuOpen = !menuOpen
			}

			// 检查菜单项点击
			for i := range itemClicks {
				if itemClicks[i].Clicked(gtx) {
					editor.SetText("选择了: " + menuItems[i])
					menuOpen = false
				}
			}
			if btn.Clicked(gtx) {
				editor.SetText("btn click")
			}
			if checkbox.Update(gtx) {
				editor.SetText("checkbox:" + strconv.FormatBool(checkbox.Value))
			}
			// 布局开始
			layout.Flex{
				Axis: layout.Horizontal,
			}.Layout(gtx,
				layout.Rigid(func(gtx layout.Context) layout.Dimensions {
					return layout.Flex{
						Axis: layout.Vertical,
					}.Layout(gtx,
						layout.Rigid(material.Button(th, &menuBtn, "菜单").Layout),
						layout.Rigid(material.Button(th, &btn, "按钮").Layout),
					)
				}),

				// 添加下拉菜单
				layout.Rigid(func(gtx layout.Context) layout.Dimensions {
					if !menuOpen {
						return layout.Dimensions{}
					}
					return layout.Inset{Top: unit.Dp(10)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
						list := widget.List{}
						list.Axis = layout.Vertical
						return material.List(th, &list).Layout(gtx, len(menuItems), func(gtx layout.Context, i int) layout.Dimensions {
							return material.Button(th, &itemClicks[i], menuItems[i]).Layout(gtx)
						})
					})
				}),

				layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
					return layout.Flex{
						Axis: layout.Vertical,
					}.Layout(gtx,
						layout.Rigid(material.CheckBox(th, &checkbox, "勾选我").Layout),
						layout.Rigid(material.Editor(th, &editor, "编辑器").Layout),
					)
				}),
				layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
					return layout.Inset{
						Top:    unit.Dp(20),
						Bottom: unit.Dp(10),
						Left:   unit.Dp(10),
						Right:  unit.Dp(10),
					}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
						return material.Editor(th, &termOutput, "终端输出").Layout(gtx)
					})
				}),
				layout.Rigid(func(gtx layout.Context) layout.Dimensions {
					return layout.Inset{
						Top:   unit.Dp(100),
						Left:  unit.Dp(10),
						Right: unit.Dp(10),
					}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
						// 设置最小高度
						return layout.Flex{
							Axis: layout.Horizontal,
						}.Layout(gtx,
							layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
								return material.Editor(th, &termInput, "终端输入").Layout(gtx)
							}),
							layout.Rigid(material.Button(th, &termBtn, "执行").Layout),
						)
					})
				}),
			)
			e.Frame(gtx.Ops)
		}
	}
}
